import { NestFactory } from '@nestjs/core';
import * as compression from 'compression';
import * as csurf from 'csurf';
import * as rateLimit from 'express-rate-limit';
import { CrudConfigService } from '@nestjsx/crud';

CrudConfigService.load({
  query: {
    limit: 20,
    maxLimit:100,
    //cache: 2000,
    alwaysPaginate:true
  },
  routes: {
    updateOneBase: {
      allowParamsOverride: true,
    },
    deleteOneBase: {
      returnDeleted: true,
    },
    only:['getOneBase','getManyBase']
  },
  //params:{
  //  id:{
  //    primary:true,
    //  disabled:true
  //  }
  //}
});

import { AppModule } from './app.module';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from "@nestjs/common";
import { ConfigService } from './config/config.service';

async function bootstrap() {
  const app = await NestFactory.create(AppModule,{
  //  logger: ["error","warn"]
  });

  const config=new ConfigService('src/env/project.env');

  app.useGlobalPipes(new ValidationPipe({
    disableErrorMessages: false,
    whitelist: true,
    forbidNonWhitelisted:false,
    skipMissingProperties:true,
  }));
  const options = new DocumentBuilder().addBearerAuth()
    .setTitle('Nide Shop Apis')
    .setDescription('Nide Shop Apis')
    .setVersion('1.0')
    .addTag('goods',"商品")
    .addTag('category',"分类")
    .addTag('brand',"制造商")
    .addTag('auth',"认证")
    .addTag('user',"用户")
    .addTag('ad',"轮播图")
    .addTag('bulletin',"公告")
    .addTag('email',"邮箱")
    .addTag('user-coupon',"用户优惠卷")
    .addTag('topic',"主题")
    .addTag("goods-attribute","商品属性")
    .addTag("goods-specification","商品规格")
    .addTag("goods-issue","商品问题")
    .addTag("comment","评论")
    .addTag("coupon-goods","优惠商品")
    .addTag("order","订单")
    .addTag("order-goods","订单商品")
    .addTag("cart","购物车")
    .addTag("product","产品")
    .addTag("footprint","足迹")
    .addTag("address","地址")
    .addTag("feedback","反馈")
    .addTag("alipay","支付宝支付")
    .addTag("collect","收藏")
    .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api', app, document);

  //app.setGlobalPrefix('/api');
  app.enableCors();
  //app.use(csurf())
  app.use(compression());
  app.use(rateLimit({
    windowMs:15*60*1000,//15 minutes
    max:100,//limit each IP  to 100 requests per windowMs
  }));
  await app.listen(parseInt(config.get('port')),'0.0.0.0');
}
bootstrap();
